﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.WindowsAzure.ServiceRuntime;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;
using System.Collections.Generic;
using System.Net;

using System.IO;

namespace Ciripescu_WebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //System.Diagnostics.Trace.TraceInformation("Default page loaded");
            //try
            //{
            //    throw new Exception("Dummy ex");
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.TraceError(ex.ToString());
            //}

            if (Request.QueryString["invitationId"] != null)            
                Response.Cookies.Add(new HttpCookie("invitationId", Request.QueryString["invitationId"]));
            if (Session["UserName"] != null)
                Response.Redirect("/home/");
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Page.Form.DefaultButton = btnSignin.UniqueID;
            //HttpCookie cookie = Request.Cookies["ciripesci.ro"];
           // test1();
        }



        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            UserManager usrManager = new UserManager();
            User usr = usrManager.GetItem(txtUsername.Text, txtPassword.Text);
            if (usr != null)
            {
                if (Session["UserName"] != null)
                    Session.Remove("UserName");

                //HttpCookie ciripCookie = new HttpCookie("ciripescu.ro");
                //ciripCookie.Expires    = DateTime.Now.AddYears(10);
                //ciripCookie["s"] = Utils.MD5Crypt(txtPassword.Text);
                //ciripCookie["u"] = txtUsername.Text;
                //Response.Cookies.Add(ciripCookie);

                Session.Add("UserName", usr.GetUserName());
                if (usr.Group == (int)UserGroup.Admin)
                {
                    Session.Add("IsAdmin", true);
                    Response.Redirect("/admin/");
                }
                else
                    Response.Redirect("/home/");
                
            }
            else
                divError.InnerHtml = Properties.Resources.ErrInvalidLogin;
        }

        protected void btnRegister_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/join/");
        }

      

        

        /*protected void Button1_Click(object sender, EventArgs e)
        {
            
            int nbEmails = Convert.ToInt32(TextBox1.Text);
            MailQueue mq = new MailQueue();
            double timp = MailQueue.HoursNeededToSendEmails(nbEmails);
            divError.InnerHtml = timp.ToString() + " ore";
        }*/


    }
}
